import * as TYPES from "../action-types";
import { getTopList, getRankingList } from "services/rank.js";

let actions = {
  getTopList: () => {
    return async (dispatch, getState) => {
      const res = await getTopList();
      const currentIdex = getState().getIn(["rank", "rankCurrenIdx"]);
      const id = res.list[currentIdex].id;
      dispatch(actions.getRankDetail(id));
      dispatch({
        type: TYPES.RANK_TIT_LIST,
        payload: res.list,
      });
    };
  },
  getRankCuIdx: (idx) => {
    return async (dispatch, getState) => {
      const rankTitList = getState().getIn(["rank", "rankTitList"]);
      const rankCurrenIdx = getState().getIn(["rank", "rankCurrenIdx"]);
      if (idx === rankCurrenIdx) return;
      dispatch(actions.getRankDetail(rankTitList[idx].id));
      dispatch({
        type: TYPES.RANK_CURRENT_IDX,
        payload: idx,
      });
    };
  },
  getRankDetail: async (id) => {
    const res = await getRankingList({ id });
    return {
      type: TYPES.RANK_DETAIL_DATA,
      payload: res,
    };
  },
};

export default actions;
